package com.hsgene.order.controller.reconciliation;

import com.hsgene.common.util.result.ApiResult;
import com.hsgene.order.constants.ReconciliationErrorCode;
import com.hsgene.order.domain.reconciliation.ReconciliationConditionDto;
import com.hsgene.order.domain.reconciliation.ReconciliationListTotalInfoDto;
import com.hsgene.order.domain.reconciliation.ReconciliationParamsInfoDto;
import com.hsgene.order.domain.reconciliation.ReconciliationPayAgencyInfo;
import com.hsgene.order.service.ReconciliationService;
import com.hsgene.product.applets.goods.AppletsErrorCode;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 业务对账
 *
 * @author wxf
 * @date 2018/11/7 15:28
 **/
@RestController
@RequestMapping("/v1.0/genetic/testing/orders/reconciliation")
public class ReconciliationController {
    /**
     * Log
     */
    private static Logger LOGGER = LogManager.getLogger(ReconciliationController.class);

    @Resource
    private ReconciliationService reconciliationService;

    /**
     * 业务对账列表
     *
     * @param conditionDto
     * @return
     */
    @GetMapping
    public ApiResult<ReconciliationListTotalInfoDto> getReconciliation(ReconciliationConditionDto conditionDto) {
        try {
            Integer page = conditionDto.getPage();
            Integer size = conditionDto.getSize();
            if (page == null || size == null || size == 0) {
                return ApiResult.fail(AppletsErrorCode.SIZA_IS_NOT_NULL);
            }
            if (page < 1) {
                page = 1;
            }
            if (StringUtils.isEmpty(conditionDto.getSortType())) {
                conditionDto.setSortType("asc");
            }
            int offset = (page - 1) * size;
            ReconciliationListTotalInfoDto listInfos = reconciliationService.getReconciliation(conditionDto, offset,
                    size);
            return new ApiResult(200, "success", "查询列表成功", listInfos);
        } catch (Exception e) {
            LOGGER.error("get reconciliation is error...", e);
            return ApiResult.fail(ReconciliationErrorCode.LIST_IS_ERROR);
        }
    }

    /**
     * 获取支付机构信息
     *
     * @return
     */
    @GetMapping("pay/agency")
    public ApiResult<List<ReconciliationPayAgencyInfo>> getPayAgencyInfo() {
        List<ReconciliationPayAgencyInfo> payAgencyInfos = reconciliationService.getPayAgencyInfo();
        return new ApiResult<>(200, "success", "获取支付机构成功", payAgencyInfos);
    }

    /**
     * 执行对账
     *
     * @param paramsInfoDto
     * @return
     */
    @PostMapping("excute")
    public ApiResult excuteReconciliation(@RequestBody ReconciliationParamsInfoDto paramsInfoDto) {
        try {
            if (paramsInfoDto == null || paramsInfoDto.getPaySerialNumber() == null || paramsInfoDto
                    .getPaySerialNumber().isEmpty()) {
                return ApiResult.fail(ReconciliationErrorCode.PARAMS_IS_NOT_NULL);
            }
            Boolean flag = reconciliationService.excuteReconciliation(paramsInfoDto.getPaySerialNumber(), new Date());
            if (flag) {
                return new ApiResult(200, "success", "对账成功", flag);
            }
            return ApiResult.fail(ReconciliationErrorCode.RECONCILIATION_IS_ERROR);
        } catch (Exception e) {
            LOGGER.error("reconciliation task is error...", e);
            return ApiResult.fail(ReconciliationErrorCode.RECONCILIATION_IS_ERROR);
        }
    }
}
